package ink.xiaobaibai.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import ink.xiaobaibai.bo.DeviceOpenInfoBo;
import ink.xiaobaibai.bo.UserAndDep;
import ink.xiaobaibai.entity.OzoneDevice;
import ink.xiaobaibai.vo.UserByDeviceVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-22
 */
public interface OzoneDeviceDao extends BaseMapper<OzoneDevice> {

    @Select("SELECT oz.admin_user_id AS userId,u.department_id AS depId FROM ozone_device oz LEFT JOIN admin_user u ON u.`id`=oz.admin_user_id WHERE oz.id=#{deviceId}")
    public UserAndDep getAdminUserAndDepByDeviceId(@Param("deviceId") Integer deviceId);

    public Integer getListByAdminCount(@Param("userIdList") List<Integer> adminUserId, @Param("provinceCity") String provinceCity, @Param("status") String status);

    public List<OzoneDevice> getListByAdmin(@Param("limitStart") Integer limitStart, @Param("limitEnd") Integer limitEnd, @Param("userIdList") List<Integer> adminUserId, @Param("provinceCity") String provinceCity, @Param("status") String status);

    @Select("SELECT u.id AS user_id,u.user_name,u.user_number,u.department_id,u.user_phone,d.department_name FROM admin_user u LEFT JOIN department d ON u.`department_id`=d.`id` WHERE u.id=#{userId}")
    public UserByDeviceVo getUserInfoByUserId(@Param("userId") Integer userId);

    @Select("SELECT oz.id AS ozoneId,oz.device_code AS ozoneDeviceCode,oz.`delayed_time` AS ozoneDelayedTime,oz.fan_time AS ozoneFanTime,oz.`valve_time` AS ozoneValveTime,\n" +
            "ox.id AS oxygenId,ox.`device_code` AS oxygenDeviceCode,ox.`delayed_time` AS oxygenDelayedTime,ox.`fan_time` AS oxygenFanTime,ox.`valve_time` AS oxygenValveTime FROM ozone_device oz LEFT JOIN oxygen_device ox ON ox.id=oz.`oxygen_id` WHERE oz.id=#{deviceId}")
    public DeviceOpenInfoBo getDeviceOpeInfo(@Param("deviceId") Integer id);


    @Select("SELECT count(*) FROM admin_user WHERE id=#{userId} AND department_id=(SELECT department_id FROM admin_user WHERE id=(SELECT admin_user_id FROM ozone_device WHERE id= #{deviceId}))")
    public Integer identicalDeviceDepByUserId(@Param("userId") Integer userId, @Param("deviceId") Integer deviceId);

    @Select("SELECT count(*) FROM admin_user WHERE id=#{userId} AND department_id=(SELECT department_id FROM admin_user WHERE id=(SELECT admin_user_id FROM ozone_device WHERE device_code= #{deviceCode}))")
    public Integer identicalDeviceDepByUserIdCode(@Param("userId") Integer userId, @Param("deviceCode") String deviceCode);

    @Select("SELECT device_code FROM oxygen_device WHERE id = (SELECT oxygen_id FROM ozone_device WHERE device_code = #{deviceCode})")
    public String getOxygenCodeByDeviceCode(@Param("deviceCode") String deviceCode);

}
